package com.sun.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;


@Aspect
@Component
public class LogAspect {

    @Pointcut("execution(* com.sun.inter.Calculator.*(..))")
    private void loggingOperation(){}

    /**
     * 目标方法之前运行
     * @Before 注解，写入切片表达式
     * execution(访问权限符 返回值类型)
     */
    @Before("loggingOperation()")
    public static void logStart(JoinPoint joinPoint) {
        System.out.println(joinPoint.getSignature().getName()+"方法开始执行，参数为"+ Arrays.toString(joinPoint.getArgs()) );
    }

    /**
     * 目标方法返回结果
     */
    @AfterReturning(pointcut = "loggingOperation()",returning = "result")
    public static void logReturn(JoinPoint joinPoint,Object result) {
        System.out.println(joinPoint.getSignature().getName()+"方法最终执行完成结果是："+result);
    }

    /**
     * 目标方法异常后执行
     */
    @AfterThrowing(pointcut = "execution(* com.sun.impl.MyMathCalculator.*(..))",throwing = "ex")
    public static void logException(JoinPoint joinPoint,ArithmeticException ex){
        System.out.println(joinPoint.getSignature().getName()+ "方法执行异常，异常信息是："+ex );
    }

    /**
     * 目标方法执行完成之后
     */
    @After("execution(public * com.sun.impl.MyMathCalculator.*(..))" )
    public static void logEnd(JoinPoint joinPoint){
        System.out.println(joinPoint.getSignature().getName()+"方法执行完成");
    }
}
